"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 数据库操作系统
Case Name   : 双机场景下停止时配置文件被人为删除
Create At   : 2020/09/27
@zou_jialiang0505328126
Description :
    1、 备机停止数据库服务
    2、 备机执行以下命令删除配置文件
    3、 备机执行以下命令重建
    4、 执行以下命令恢复配置文件
    5、 备机执行以下命令重建
Expect      :
    1、 备机停止数据库服务成功
    2、 备机执行以下命令删除配置文件成功
    3、 备机执行以下命令重建失败
    4、 执行以下命令恢复配置文件成功
    5、 备机执行以下命令重建成功
History     :
    modified ：2021-1-26 by @peilinqian ；修改build调用方式，修改整个脚本；
"""

import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import RestartDbCluster
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DbsysCase119(unittest.TestCase):
    pri_sh = CommonSH('PrimaryDbUser')
    sta_1_sh = CommonSH('Standby1DbUser')
    sta_2_sh = CommonSH('Standby2DbUser')

    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('-----this is setup-----')
        self.log.info('----Opengauss_Reliability_Dbsys_Case119:初始化----')
        self.dbUserNode = Node(node='dbuser')
        self.primary_root = Node(node='PrimaryRoot')
        self.primary_dbuser = Node(node='PrimaryDbUser')
        self.standby1_root = Node(node='Standby1Root')
        self.standby1_dbuser = Node(node='Standby1DbUser')
        self.standby2_dbuser = Node(node='Standby2DbUser')
        self.com = Common()
        self.constant = Constant()

        self.conf_file = os.path.join(macro.DB_INSTANCE_PATH,
                                      macro.DB_PG_CONFIG_NAME)
        self.conf_file_bak = self.conf_file + '.baktest'

    def test_main(self):
        self.log.info('----停止备机----')
        stop_msg = self.sta_1_sh.stopDbInstance()
        self.assertIn(self.constant.GS_CTL_STOP_SUCCESS_MSG, stop_msg)

        self.log.info('----删除备机conf文件----')
        delete_cmd = f'cp -R {self.conf_file} {self.conf_file_bak};' \
            f' rm -rf {self.conf_file}'
        self.log.info(delete_cmd)
        self.standby1_dbuser.sh(delete_cmd)
        ls_file_cmd = f'ls -al {self.conf_file}'
        self.log.info(ls_file_cmd)
        ls_file_result = self.standby1_dbuser.sh(ls_file_cmd).result()
        self.log.info(ls_file_result)

        self.log.info('-----重建备机-----')
        build_result = self.sta_1_sh.buildStandby()
        self.assertIn(self.constant.BUILD_FAIL_MSG_OPEN, build_result)

        time.sleep(5)

        self.log.info('----恢复备机conf文件----')
        recover_cmd = f'cp -R {self.conf_file_bak} {self.conf_file};' \
            f' rm -rf {self.conf_file_bak}'
        self.log.info(recover_cmd)
        self.standby1_dbuser.sh(recover_cmd)
        ls_file_cmd = f'ls -al {self.conf_file}'
        self.log.info(ls_file_cmd)
        ls_file_result = self.standby1_dbuser.sh(ls_file_cmd).result()
        self.log.info(ls_file_result)

        self.log.info('-----重建备机-----')
        build_result = self.sta_1_sh.buildStandby()
        self.assertIn(self.constant.BUILD_SUCCESS_MSG, build_result)

        self.log.info('----查看主机query，同步是否正常----')
        self.pri_sh.check_location_consistency('primary', 3)
        status_result = self.sta_1_sh.getDbClusterStatus('status')
        self.assertTrue(status_result)

    def tearDown(self):
        self.log.info('----------this is tearDown---------')
        ls_path_cmd = f'ls -al {macro.DB_INSTANCE_PATH}'
        ls_path_result = self.standby1_dbuser.sh(ls_path_cmd).result()
        if macro.DB_PG_CONFIG_NAME + '.baktest' in ls_path_result:
            self.log.info('----恢复备机conf文件----')
            rm_cmd = f'rm -f {self.conf_file}'
            self.log.info(rm_cmd)
            self.standby1_dbuser.sh(rm_cmd)
            recover_cmd = f'cp -R {self.conf_file_bak} {self.conf_file};' \
                f' rm -rf {self.conf_file_bak}'
            self.log.info(recover_cmd)
            self.standby1_dbuser.sh(recover_cmd)
            ls_file_cmd = f'ls -al {self.conf_file}'
            self.log.info(ls_file_cmd)
            ls_file_result = self.standby1_dbuser.sh(ls_file_cmd).result()
            self.log.info(ls_file_result)
        self.log.info('----Opengauss_Reliability_Dbsys_Case119:执行结束----')
